package com.itheima.controller;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName AliPayController
 * @date 2022/10/13 12:24
 */
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.itheima.entity.Orders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@RestController()
public class AliPayController {
    private final String APP_ID = "2021000121669310";

    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCFnRwLSoZvqCtinpEjpjxdmzyba990t/bc/wc0CAziq4KtI8b5xGy77rsb8lK9LmgrYQSouOHDDTTFXXMjmQCG1YO6ue9lcR3QKNbSk8WshBgxJ7TqqegYOntS09kkSkSIly3hWLDr9hHVADDO+272HFMPQ55hXpi7+vJV8kVeJISDWf1eEjTABX+r53w6mjVcglDo7xR6uvUFzGkTwfyzJ+xV/Ax+ADwDE64+9A6/bMPEyyY/+2v4NWbP3oebHgGzDvHhsZqXtSB51cC7/a3j3/lt/E269RjS7+1W5Frnk55uOAiMGoBvC92Nh2fMSu/QSpKCDMQgS3puBQaLvCN3AgMBAAECggEAai4laC+sUDVTbQbYDn1/wV+pNVXveUDstEkS/11aaBOmQFOLP1fClUKtfRMAf4jOjZKG8lIiTzBCfIm8A1EGoyAwlAZI4dOXvqGFvkhisasz0nRciJPHduGauYLTL8y5U9bYRhmGV63wE9YX85TKZFi0BRBWX8ieFdziEjz6tUmn7GVlbacd5lvTKXj4zfSZy0OP252gfe73DdExk13q0181dxaWznVKAM26asp6vos5ltEKZUDJVSlGOC6av0KgmQBLqwp5j4sfHJZJRJ058cHEpu0qzQv/wV4lewI3ZwgEn997PDqdye7oWjjeMYuaoGK+ySNbbVZp3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x+/rtueTwtqqYaifQAuo38wmFnPwlxUQUspUvILmf4P5TqxLOJLx3w0foQ8vpdmA6mfPDbZhhyTheXmwTVf3YIbuU5Z4bXzgQYGdDotmv5TYkHMPw+VXQH/AnthfdXIVwIS/n3pqW/xV5zBTHmceRikcDreJ9gfG68AUhI6ZxnntXaVpTarAYAMJMB/G9gW3DN9gt6ZsW0biC/1wrmNONmut/TweM5Zos6y5hdfLBvITiO2TwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/front/page/pay-success.html";
    @RequestMapping("/alipay")
    @ResponseBody
    public void alipay(HttpServletResponse httpResponse, HttpServletRequest request1) throws IOException {

        Random r=new Random();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        Orders orders = (Orders) request1.getSession().getAttribute("orders");
        //生成随机Id
        String out_trade_no =orders.getNumber()/* UUID.randomUUID().toString()*/;
        //付款金额，必填
        String total_amount =orders.getAmount().toString()/*Integer.toString(r.nextInt(1)+1)*/;
        //订单名称，必填
        String subject ="瑞吉外卖";
        //商品描述，可空
        String body = "外卖";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}

